package com.wxf.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Component
public class AckListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag: "+deliveryTag);

        try{
            System.out.println("消息内容："+message.getBody());
            //2. 处理业务逻辑
            System.out.println("处理业务逻辑...");
            //3. 手动签收
            channel.basicAck(deliveryTag,true);
        }catch (Exception e){
            // requeue是重新放回队列，如果不设置重新放回队列，信息还是会丢失
            channel.basicNack(deliveryTag,true,true);
        }
    }
}
